package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.IWmChannelService;
import jdk.net.SocketFlow;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    /**
     * 自媒体端,查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        //调用方法直接进行查询
        List<WmChannel> list = this.list();
        return ResponseResult.okResult(list);
    }

    /**
     * 保存频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //校验请求参数
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //使用频道名称作为条件查询数据库,看看是否能够查询导数据
        WmChannel one = this.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        //判断查询结果,只有当查询不出来的时候,才新增频道
        if (one != null){
            //数据已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }else{
            //补全字段值,调用该方法进行存储
            wmChannel.setCreatedTime(new Date());
            wmChannel.setIsDefault(Boolean.TRUE);
            this.save(wmChannel);
            return ResponseResult.okResult(200,"操作成功");
        }
    }

    /**
     * 后台管理端,分页查询频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listChannel(WmChannelDto dto) {
        //校验请求参数
        if (dto.getPage() == null || dto.getSize() == null){
            //提示缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //构造分页查询条件
        Page<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        //判断请求参数中有无查询条件
        QueryWrapper<WmChannel> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            //构造条件查询条件
            queryWrapper.like("name",dto.getName());
        }
        //调用方法执行查询操作
        Page<WmChannel> pageInfo = this.page(page, queryWrapper);
        //封装结果集,创建分页结果返回对象
        PageResponseResult responseResult = new PageResponseResult();
        responseResult.setData(pageInfo.getRecords());
        responseResult.setCurrentPage(dto.getPage());
        responseResult.setSize(dto.getSize());
        responseResult.setTotal((int) pageInfo.getTotal());

        return responseResult;
    }

    /**
     * 后台管理端,修改频道
     * 启用和禁用
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //校验请求参数是否存在
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //请求参数存在则调用方法修改数据库中的数据即可
        this.updateById(wmChannel);

        return ResponseResult.okResult(wmChannel);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //校验请求参数是否存在
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id去数据库中查询出频道数据
        WmChannel wmChannel = this.getById(id);
        //判断频道的状态
        if (wmChannel.getStatus().equals(Boolean.TRUE)){
            //频道时启用状态,不能删除
            return ResponseResult.errorResult(501,"频道正在使用,不能删除");
        }
        //频道已经被禁用了,可以调方法执行删除操作
        this.removeById(id);
        return ResponseResult.okResult(200,"操作成功");
    }


}
